package cn.aj.basic.utils;

import cn.aj.basic.config.SpringConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Slf4j
public class EmailUtil {
    public static void sendEmail(String subject,String to,String text){
        ApplicationContext context = SpringConfig.getContext();
        JavaMailSender javaMailSender = context.getBean(JavaMailSender.class);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("982616996@qq.com");
            helper.setSubject(subject);
            helper.setText(text,true);
            // 5.设置收件人
            helper.setTo(to);
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.info("发送邮件失败:{}",e);
        }
    }
}
